/**
 * Copyright (c) Software Institute of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.modeler.ui.palette;

import org.eclipse.gef.requests.CreationFactory;

import edu.pku.sei.modeler.model.common.AbstractFactory;
import edu.pku.sei.modeler.model.core.MetaModelElement;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.modeler.model.structure.StructureFactory;

/**
 * @author zl
 *
 */
public class MetaCreationFactory implements CreationFactory {
	protected boolean isNode;
	protected String elementID;
	protected StructureFactory sFactory;
	protected AbstractFactory factory;
	
//	public MetaCreationFactory(boolean isNode, String elementID) {
//		this.isNode = isNode;
//		this.elementID = elementID;
//	}
	public MetaCreationFactory(boolean isNode, String elementID, AbstractFactory factory, StructureFactory sFactory) {
		this.isNode = isNode;
		this.elementID = elementID;
		this.factory = factory;
		this.sFactory = sFactory;
	}
	/* (non-Javadoc)
	 * @see org.eclipse.gef.requests.CreationFactory#getNewObject()
	 */
	public Object getNewObject() {
		MetaShapeElement shape = null;
		if(sFactory==null || factory==null)return null;
//		shape = factory.createElement(elementID, true);
//		if(elementID.equals(StructurePackage.__REFERENCENODE__)) {
//			shape = factory.createReferenceNode();
//			MetaElement model = CoreFactory.fInstance.createMetaClass();
//			shape.setElement(model);
//			return shape;
//		}
//		///////////////////////////////////////
//		//@liangjie
//		else if(elementID.equals(StructurePackage.__COMPOUNDNODE__)) {
//			shape = StructureFactory.fInstance.createMetaCompoundNode();
//			MetaElement model = CoreFactory.fInstance.createMetaClass();
//			shape.setElement(model);
//			return shape;
//		}
//		///////////////////////////////////////
		if(isNode) {
			shape = sFactory.createMetaNode();
		}
		else {
			shape = sFactory.createMetaLink();
		}
		MetaModelElement model = (MetaModelElement)factory.createElement(elementID, true);
		shape.setModelElement(model);
//		if(CorePackage.__TEMPLATENODE__.equals(elementID)) {
//			PortLocatorAdapter locator = StructureFactory.fInstance.createPortLocatorAdapter();
//			locator.setDirection(PortLocatorAdapter.SURROUNDING);
//			locator.setNumPerSide(2);
//			MetaLocationManager lMgr = StructureFactory.fInstance.createMetaLocationManager();
//			lMgr.setManageClass(TemplatePort.class.getName());
//			lMgr.setLocator(locator);
//			((MetaNode)shape).getLocationMgrs().add(lMgr);
//		}
//		if(CorePackage.__METACLASS__.equals(elementID)) {
//			switch(shape_type) {
//			case 0:
//				break;
//			case 1:
//				((MetaClass)model).setInterface(true);
//				break;
//			}
//		}
		return shape;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.requests.CreationFactory#getObjectType()
	 */
	public Object getObjectType() {
		return elementID;
	}

}
